package view;

import javax.swing.*;

import controller.AddController;
import controller.ByGroupController;
import controller.ByNameController;
import controller.LastAddedController;
import controller.OrderByNameController;
import controller.SelectController;
import model.Contact;

import java.awt.*;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * This class display the list of the contacts and the button (search, by name, by group, add) at the left of the window
 * @author Marie Morin
 *
 */

public class ContactWindow extends JPanel{
	
	private ArrayList<Contact> contactsList;
	private ArrayList<String> contactsAname = new ArrayList<String>();
	private ArrayList<String> contactsGroup = new ArrayList<String>();
	private MainWindow w;
	private JLabel contacts = new JLabel("Contacts :");
	private JButton add = new JButton("+");
	private JButton group = new JButton("By group");
	private JButton name = new JButton("By name");
	private JButton lastAdded = new JButton("Last added");
	private JButton clear = new JButton("Clear");
	private JTextField search = new JTextField("Search");
	
	private SelectController s;
	private AddController sc;
	private OrderByNameController sw;
	private ByGroupController sg;
	private ByNameController sn;
	private LastAddedController sd;
	
	
	public ContactWindow(MainWindow w) {
		
		this.w = w;
		s = new SelectController(w);
		contactsList = s.getContactsList();
		

		this.setContactWindow();

	

	}
	
	public ContactWindow(MainWindow w, ArrayList<Contact> contactsList) {
		
		this.w = w;
		s = new SelectController(w);
		this.contactsList = contactsList;
		
		this.setContactWindow();

	}
	

	public void setContactWindow(){
		
		this.setLayout(null);
		this.setLayout(new BorderLayout());
		
		if (contactsList.size()>0)
		{
			for (int i=0; i< contactsList.size(); i++)
			{
				contactsAname.add(contactsList.get(i).getAname());
			}
		}
		
		
		JList list = new JList(contactsAname.toArray());
		
		JScrollPane scrollpane = new JScrollPane(list);
		
		this.add(scrollpane, BorderLayout.CENTER);
		

		list.addListSelectionListener(this.s);
		
		this.displayButton();
		
		
		
	}
	
	/**
	 * Display the buttons of the list on the left of the page (search, byname, bygroup, add, etc...)
	 */
	public void displayButton(){
		
		JPanel p = new JPanel();
		p.setLayout(new BorderLayout());
		

		p.add(add, BorderLayout.EAST);
		p.add(lastAdded, BorderLayout.CENTER);
		
		JPanel p1 = new JPanel();
		p1.setLayout(new BorderLayout());
		

		p1.add(search, BorderLayout.NORTH);
		p1.add(group, BorderLayout.CENTER);
		p1.add(name, BorderLayout.WEST);
		p1.add(clear, BorderLayout.EAST);

		
		p.add(p1, BorderLayout.NORTH);
		
		this.add(p,BorderLayout.NORTH);
				
		sw = new OrderByNameController(w);
		clear.addActionListener(this.sw);
		
		sc = new AddController(w);
		add.addActionListener(this.sc);
		
		sg = new ByGroupController(w, this);
		group.addActionListener(this.sg);
		
		sn = new ByNameController(w, this);
		name.addActionListener(this.sn);
		
		sd = new LastAddedController(w);
		lastAdded.addActionListener(this.sd);
		
	}

	public String getSearch() {
		return search.getText();
	}

	public void setSearch(JTextField search) {
		this.search = search;
	}


	
}

